Skip to content

[math] Make TRandom compatible with UniformRandomBitGenerator - Issue # 7979#22508

Merged
guitargeek merged 3 commits into
root-project:masterfrom
medeirosdev:trandom-std-interface
Jun 12, 2026
Merged

[math] Make TRandom compatible with UniformRandomBitGenerator - Issue # 7979#22508
guitargeek merged 3 commits into
root-project:masterfrom
medeirosdev:trandom-std-interface

Conversation

@medeirosdev

Copy link
Copy Markdown
Contributor

TRandom and its subclasses can now be used directly with standard library algorithms like std::shuffle and std::uniform_int_distribution.

This is done by adding the three required members to TRandom: result_type, min(), max(), and operator(). The operator() delegates to the virtual Rndm(), so polymorphism is preserved across all TRandom subclasses.

Fixes #7979

This Pull request:

Changes or fixes:

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

This PR fixes #

TRandom and its subclasses can now be used directly with standard
library algorithms like std::shuffle and std::uniform_int_distribution.

This is done by adding the three required members to TRandom:
result_type, min(), max(), and operator(). The operator() delegates
to the virtual Rndm(), so polymorphism is preserved across all
TRandom subclasses.

Fixes root-project#7979
Comment thread math/mathcore/inc/TRandom.h Outdated
Comment thread math/mathcore/inc/TRandom.h Outdated
@ferdymercury

Copy link
Copy Markdown
Collaborator

also to discuss whether this can be unified with tmva/tmva/inc/TMVA/Tools.h to avoid code duplicities

- Convert // NOTE: comment to Doxygen /// \note format in TRandom.h
- Make TRandom::operator()() virtual so subclasses can override it
- Add TRandom2::operator()() returning raw Tausworthe XOR output,
  avoiding the double round-trip through Rndm() / kScale
- Add TRandom3::operator()() returning raw Mersenne Twister output,
  same motivation as above
- Extend testMathRandom test5() to exercise TRandom2 as a URBG

TRandom1 (RANLUX) has a float-based internal state with no meaningful
raw integer path, so it keeps the base-class fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@medeirosdev

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

Addressed all three points:

  • Converted the // NOTE: comment to Doxygen /// \note format
  • Made operator()() virtual in TRandom
  • Added operator()() overrides in TRandom2 and TRandom3 that return the raw integer output directly, skipping the double round-trip

For TRandom1 (RANLUX), the internal state is float-based (fFloatSeedTable), so there's no raw integer to expose — the base class fallback is the best we can do there.

@medeirosdev

medeirosdev commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

also to discuss whether this can be unified with tmva/tmva/inc/TMVA/Tools.h to avoid code duplicities

The main difference is that our approach embeds the interface directly in TRandom, so any existing TRandom* pointer works as a URBG without wrapping. It also lets subclasses override operator()() to return raw integers directly (as done here for TRandom2 and TRandom3), which TMVA::RandomGenerator doesn't do, it goes through Integer() which still uses a double internally.

i'm open to explore that in a follow-up PR if that's the preferred direction.

Comment thread math/mathcore/src/TRandom2.cxx
Avoids duplicating the Tausworthe/Mersenne Twister state-advance logic.
Rndm() now calls operator()() to get the raw integer and then scales it
to a double, keeping a single source of truth for the generator core.
The TAUSWORTHE macro is moved to file scope so RndmArray can still use it.
Comment thread math/mathcore/src/TRandom2.cxx

@ferdymercury ferdymercury left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@medeirosdev

medeirosdev commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@guitargeek @hageboeck hey guys! could you check this pr? please

@github-actions

Copy link
Copy Markdown

Test Results

    22 files      22 suites   3d 16h 35m 49s ⏱️
 3 862 tests  3 804 ✅   0 💤 58 ❌
77 198 runs  77 034 ✅ 105 💤 59 ❌

For more details on these failures, see this check.

Results for commit b59d5c7.

@guitargeek guitargeek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this feature and to @ferdymercury for the review!

@guitargeek guitargeek merged commit 15903c3 into root-project:master Jun 12, 2026
28 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make TRandom compatible with the std UniformRandomBitGenerator interface

5 participants